package com.atguigu.gmall.auth.controller;

import com.atguigu.gmall.auth.service.AuthService;
import com.atguigu.gmall.common.bean.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description:
 * @author: xie.zh
 * @create: 2021.03.29   11:52
 * @version: 1.0
 */
@Controller
public class AuthController {

    @Autowired
    private AuthService authService;

    /**
     * 跳转登录页面 并携带登录成功后的url
     *
     * @param returnUrl
     * @param model
     * @return
     */
    @GetMapping("toLogin.html")
    public String toLogin(@RequestParam("returnUrl") String returnUrl, Model model) {
        model.addAttribute("returnUrl", returnUrl);
        return "login";
    }

    @PostMapping("login")
    public String login(@RequestParam("loginName") String loginName,
                        @RequestParam("password") String password,
                        @RequestParam("returnUrl") String returnUrl,
                        HttpServletRequest request,
                        HttpServletResponse response) {
        authService.login(loginName, password, request, response);
        return "redirect:" + returnUrl;
    }
}
